package com.ruoyi.mateproduction.service.impl;

import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mateproduction.domain.MateProductionRecord;
import com.ruoyi.mateproduction.domain.MateProductionRecordModel;
import com.ruoyi.mateproduction.domain.MateRecordInformation;
import com.ruoyi.mateproduction.domain.MateTransfer;
import com.ruoyi.mateproduction.mapper.MateTransferMapper;
import com.ruoyi.mateproduction.mapper.MtProductionMapper;
import com.ruoyi.mateproduction.mapper.MtRecordMapper;
import com.ruoyi.mateproduction.service.MtProductionRecordService;
import com.ruoyi.mtAudit.domain.MtAudit;
import com.ruoyi.mtAudit.mapper.MtAuditMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author
 * @Version 1.0
 */
@Service
public class MtProductionRecordServiceImpl implements MtProductionRecordService {

    @Autowired
    MtRecordMapper mtRecordMapper;

    @Autowired
    MtAuditMapper mtAuditMapper;

    @Autowired
    MtProductionMapper mtProductionMapper;

    @Autowired
    MateTransferMapper mateTransferMapper;

    @Override
    public int addMateRecord(MateProductionRecord mateProductionRecord) {

        int num = 0;
        Map<String, Object> resMap = new HashMap<>();
        try {

            num = mtRecordMapper.addMateRecord(mateProductionRecord);
            if (mateProductionRecord.getMateProductionRecordModels() != null) {
                for (MateProductionRecordModel mateProductionRecordModel : mateProductionRecord.getMateProductionRecordModels()) {
                    mateProductionRecordModel.setDeptId(SecurityUtils.getDeptId());
                    mateProductionRecordModel.setMtRecordId(mateProductionRecord.getMtRecordId());
                    mtRecordMapper.addMateRecordModel(mateProductionRecordModel);
                }
            }

            if (mateProductionRecord.getMateRecordInformations() != null) {
                for (MateRecordInformation mateRecordInformation : mateProductionRecord.getMateRecordInformations()) {
                    mateRecordInformation.setMtRecordId(mateProductionRecord.getMtRecordId());
                    mateRecordInformation.setDeptId(SecurityUtils.getDeptId());
                    mtRecordMapper.addMateRecordInformation(mateRecordInformation);
                }
            }

            MtAudit mtAudit = new MtAudit();
            mtAudit.setCompanyId(SecurityUtils.getDeptId());
            mtAudit.setOrderId(mateProductionRecord.getMtRecordId());
            mtAudit.setZjName(mateProductionRecord.getMateProductionRecordModels().get(0).getOperatorName());
            mtAudit.setOrderType("8");
            mtAudit.setOrderNum(mateProductionRecord.getRecordOrderNum());
            mtAudit.setAuditStatus("0");
            mtAudit.setAuditBy(SecurityUtils.getLoginUser().getNickName());
            mtAuditMapper.insertMtAudit(mtAudit);

            MateTransfer mateTransfer = new MateTransfer();
            mateTransfer.setTransferId(mateProductionRecord.getTransferId());
            mateTransfer.setIsCreateRecord("1");
            mateTransferMapper.updateMateTransfer(mateTransfer);

        } catch (Exception e) {
            mtRecordMapper.removeMtRecord(mateProductionRecord.getMtRecordId());
            mtRecordMapper.removeMtRecordModel(mateProductionRecord.getMtRecordId());
            mtRecordMapper.removeMtRecordInformation(mateProductionRecord.getMtRecordId());

            resMap.put("orderId", mateProductionRecord.getMtRecordId());
            resMap.put("orderType", "8");
            mtAuditMapper.deleteMtAuditById(resMap);

            MateTransfer mateTransfer = new MateTransfer();
            mateTransfer.setTransferId(mateProductionRecord.getTransferId());
            mateTransfer.setIsCreateRecord("0");
            mateTransferMapper.updateMateTransfer(mateTransfer);
        }
        return num;
    }

    @Override
    public List<MateProductionRecord> recordList(MateProductionRecord mateProductionRecord) {
        return mtRecordMapper.recordList(mateProductionRecord);
    }

    @Override
    public MateProductionRecord selectRecord(Long mtRecordId) {
        return mtRecordMapper.selectRecord(mtRecordId);
    }

    @Override
    public int editRecord(MateProductionRecord mateProductionRecord) {

        int num = 0;
        if (mateProductionRecord.getMateProductionRecordModels() != null) {
            mtRecordMapper.removeMtRecordModel(mateProductionRecord.getMtRecordId());
            mtRecordMapper.removeMtRecordInformation(mateProductionRecord.getMtRecordId());
            for (MateProductionRecordModel mateProductionRecordModel : mateProductionRecord.getMateProductionRecordModels()) {
                mateProductionRecordModel.setDeptId(SecurityUtils.getDeptId());
                mateProductionRecordModel.setMtRecordId(mateProductionRecord.getMtRecordId());
                mtRecordMapper.addMateRecordModel(mateProductionRecordModel);

            }
            for (MateRecordInformation mateRecordInformation : mateProductionRecord.getMateRecordInformations()) {
                mateRecordInformation.setDeptId(SecurityUtils.getDeptId());
                mateRecordInformation.setMtRecordId(mateProductionRecord.getMtRecordId());
                mtRecordMapper.addMateRecordInformation(mateRecordInformation);
            }
        }

        num = mtRecordMapper.editRecord(mateProductionRecord);
        return num;
    }

    @Override
    public void remove(Long mtRecordId) {
        Map<String, Object> resMap = new HashMap<>();
        resMap.put("orderId", mtRecordId);
        resMap.put("orderType", "8");
        MateProductionRecord mateProductionRecord = mtRecordMapper.selectRecord(mtRecordId);
        MateTransfer mateTransfer = new MateTransfer();
        mateTransfer.setTransferId(mateProductionRecord.getTransferId());
        mateTransfer.setIsCreateRecord("0");
        mateTransferMapper.updateMateTransfer(mateTransfer);
        mtAuditMapper.deleteMtAuditById(resMap);
        mtRecordMapper.removeMtRecord(mtRecordId);
        mtRecordMapper.removeMtRecordModel(mtRecordId);
        mtRecordMapper.removeMtRecordInformation(mtRecordId);
    }

    @Override
    public List<MateProductionRecord> auditRecord(Long deptId) {
        return mtRecordMapper.auditRecord(deptId);
    }

    @Override
    public List<MateProductionRecordModel> mateProductionRecordModels(Long mtRecordId) {
        return mtRecordMapper.mateProductionRecordModels(mtRecordId);
    }
}
